package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.entity.Check;
import com.neuedu.entity.CheckRequest;
import com.neuedu.entity.Doctor;
import com.neuedu.entity.Register;
import com.neuedu.mapper.CheckMapper;
import com.neuedu.mapper.CheckRequestMapper;
import com.neuedu.mapper.DoctorMapper;
import com.neuedu.mapper.RegisterMapper;
import com.neuedu.service.CheckRequestService;
import com.neuedu.vo.CheckRequestCondition;
import com.neuedu.vo.CheckRequestVO;
import com.neuedu.vo.CheckResultVO;
import com.neuedu.vo.CheckVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CheckRequestServiceImpl extends ServiceImpl<CheckRequestMapper, CheckRequest> implements CheckRequestService {

    @Resource
    private CheckRequestMapper checkRequestMapper;

    @Resource
    private CheckMapper checkMapper;

    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private RegisterMapper registerMapper;

    @Transactional
    @Override
    public boolean addCheckRequest(String registerId, String checkIds) {

        //拆分使用逗号分隔的检查项目编号的字符串到数组中
        String[] checkIdArray = checkIds.split(",");

        if(checkIdArray.length==0){
            return false;
        }

        //遍历检查项目编号的数组
        for (String checkIdString : checkIdArray) {

            //转成整型
            int checkId = Integer.parseInt(checkIdString);

            //创建并填充检查申请对象
            CheckRequest checkRequest = new CheckRequest();

            checkRequest.setRegisterId(registerId);
            checkRequest.setCheckId(checkId);
            checkRequest.setCreateTime(new Date());
            checkRequest.setStatus(1);

            //添加到数据库中
            if(!this.save(checkRequest)){
                return false;
            }

        }

        return true;
    }

    @Override
    public List<CheckVO> getCheckList(String registerId) {
        //方式1---使用xml映射文件自动封装VO对象
        //return checkRequestMapper.findCheckList(registerId);

        //方式2---在业务层手动封装VO对象
        List<CheckVO> checkVOList = new ArrayList<>();

        List<CheckRequest> checkRequestList = this.query().eq("register_id", registerId).eq("status", 1).list();

        for (CheckRequest checkRequest : checkRequestList) {

            //创建并填充VO对象
            CheckVO checkVO = new CheckVO();

            BeanUtils.copyProperties(checkRequest, checkVO);

            //填充检查名称和检查费用
            Check check = checkMapper.selectById(checkRequest.getCheckId());

            checkVO.setCheckName(check.getCheckName());
            checkVO.setCheckAmount(check.getCheckAmount());

            //添加到VO列表中
            checkVOList.add(checkVO);
        }

        return checkVOList;
    }

    @Override
    public boolean feeCheckList(String registerId) {

        UpdateWrapper<CheckRequest> wrapper = new UpdateWrapper<>();

        wrapper.eq("register_id", registerId);
        wrapper.set("fee_time",  new Date());
        wrapper.set("status", 2);

        return this.update(wrapper);
    }

    @Override
    public List<CheckRequestVO> getCheckList(CheckRequestCondition checkRequestCondition) {

        //方式1---使用xml映射文件自动封装VO对象
        //checkRequestCondition.setRegisterId("%" + checkRequestCondition.getRegisterId() + "%");
        //return checkRequestMapper.findCheckRequestList(checkRequestCondition);

        //方式2---在业务层手动封装VO对象
        List<CheckRequestVO> checkRequestVOList = new ArrayList<>();

        //根据病历号和检查状态查询检查申请列表
        List<CheckRequest> checkRequestList = null;

        if(checkRequestCondition.getStatus()==null){
            checkRequestList = this.query().like("register_id", checkRequestCondition.getRegisterId()).list();
        }else{
            checkRequestList = this.query().like("register_id", checkRequestCondition.getRegisterId()).eq("status", checkRequestCondition.getStatus()).list();
        }

        //遍历检查申请列表
        for (CheckRequest checkRequest : checkRequestList) {

            //获取检查申请对应的检查项目
            Check check = checkMapper.selectById(checkRequest.getCheckId());

            //获取当前登录医生对应的科室
            Doctor doctor = doctorMapper.selectById(checkRequestCondition.getDoctorId());

            System.out.println(check.getDeptId());
            System.out.println(doctor.getDeptId());
            //如果检查项目和医生对应的科室编号一致，则添加到最终的结果列表中
            if(check.getDeptId()==doctor.getDeptId()){

                System.out.println(1);
                //创建并填充VO对象
                CheckRequestVO checkRequestVO = new CheckRequestVO();

                BeanUtils.copyProperties(checkRequest, checkRequestVO);

                //填充患者姓名
                Register register = registerMapper.selectById(checkRequest.getRegisterId());
                checkRequestVO.setPatientName(register.getPatientName());

                //填充检查名称
                checkRequestVO.setCheckName(check.getCheckName());

                //添加到VO列表中
                checkRequestVOList.add(checkRequestVO);
            }

        }

//        for (CheckRequestVO checkRequestVO : checkRequestVOList) {
//            System.out.println(checkRequestVO + "1");
//        }

        return checkRequestVOList;
    }

    @Override
    public boolean modifyCheckResult(CheckRequest checkRequest) {

        UpdateWrapper<CheckRequest> wrapper = new UpdateWrapper<>();

        wrapper.eq("request_id", checkRequest.getRequestId());

        wrapper.set("doctor_id", checkRequest.getDoctorId());
        wrapper.set("check_time", new Date());
        wrapper.set("check_result", checkRequest.getCheckResult());
        wrapper.set("check_photo", checkRequest.getCheckPhoto());
        wrapper.set("status", 3);

        return this.update(wrapper);
    }

    @Override
    public List<CheckResultVO> getCheckResult(String registerId) {

        //方式1---使用xml映射文件自动封装VO对象
        return checkRequestMapper.findCheckResult(registerId);

    }
}
